(function () {
    initLoad();
})();

// 全局常量变量 start
const defaultHeaders = { "Content-Type": "application/json;charset=UTF-8" }

const defaultOptions = {
    timeout: 60000, //超时时间设置，单位毫秒
    url: "", // 请求的url
    type: "get", // 请求的类型
    dataType: "json", // 接收数据类型
    async: true, // 异步请求
    cache: false, // 浏览器缓存
    headers: defaultHeaders, // 请求头设置
};
// end

// 初始化调用
function initLoad() {
    $("#btn").click(function (e) {
        const getHttpUrl = "https://file.rayooec.cn/pem/prod-api/sysDictType/allDict";
        ajaxRequest({
            url:getHttpUrl, 
            type: 'get',
            data: {"pageNum":1,"pageSize":10}
        });


        // const postHttpUrl = "https://file.rayooec.cn/pem/prod-api/sysDictType/pageList";
        // ajaxRequest({
        //     url: postHttpUrl,
        //     type: 'post',
        //     headers: {
        //         ...defaultHeaders,
        //         "Authorization": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTm8iOiJhZG1pbiIsImV4cCI6MTY2MDczNDM4MH0.05gDHCEjkyqWqM-ilwVrTFqDoaiWuW0CbuhZWplKcDM" 
        //     },
        //     data: {"baseEntity":{},"pageNum":1,"pageSize":10}
        // })

    });
}

function ajaxRequest(options) {
    const activeOptions = Object.assign(defaultOptions, options);
    const { type } = activeOptions;
    switch (type) {
        case "get":
            activeOptions.data = activeOptions.params || activeOptions.data;
            break;
        case "post":
            activeOptions.data = JSON.stringify(activeOptions.data);
            break;
    }

    console.log("params", activeOptions);
    showLoading();

    return new Promise((resolve, reject) => {
        $.ajax({
            ...activeOptions,
            success: function (result) {
                console.log("success: ", result);
                resolve(result);
            },
            error: function (error) {
                console.log("error: ", error);
                reject(error)
            },
            complete: function (params) {
                console.log("complete: ", params);
                hideLoading();
            },
        });
    })
}


// 编译全局loading dom
function compileLoading() {
    return $(`<div id="loadingId">
        <div style="" class="heart-loader">
            <span style=""></span>
        </div>
        <style>
            #loadingId{
                width: 100%;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
                z-index: 999;
                background: rgba(0,0,0,0.2);
            }
            .heart-loader{
                background-color: #f44336;
                width: 50px;
                height: 50px;
                position: relative;
                -webkite-transform: rotate(45deg);
                -webkit-transform: rotate(45deg);
                -ms-transform: rotate(45deg);
                transform: rotate(45deg);
                -webkit-animation: pulsate 1s infinite;
                animation: pulsate 1s infinite;
            }
            .heart-loader::before, .heart-loader::after {
                display: inline-block;
                content: "";
                width: 50px;
                height: 50px;
                background-color: #f44336;
                position: absolute;
                border-radius: 50%;
            }

            .heart-loader::before {
              top: 0px;
              left: -25px;
            }
            .heart-loader::after {
              top: -25px;
              left: -0px;
            }
            
            @-webkit-keyframes pulsate {
                0% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                5% {
                -webkit-transform: rotate(45deg) scale(0.9);
                transform: rotate(45deg) scale(0.9);
                }
                10% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                15% {
                -webkit-transform: rotate(45deg) scale(1);
                transform: rotate(45deg) scale(1);
                }
                50% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                100% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
            }
            
            @keyframes pulsate {
                0% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                5% {
                -webkit-transform: rotate(45deg) scale(0.9);
                transform: rotate(45deg) scale(0.9);
                }
                10% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                15% {
                -webkit-transform: rotate(45deg) scale(1);
                transform: rotate(45deg) scale(1);
                }
                50% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
                100% {
                -webkit-transform: rotate(45deg) scale(0.8);
                transform: rotate(45deg) scale(0.8);
                }
            }
        </style>
    </div>`);
}


// 显示全局加载
function showLoading() {
    $("body").append(compileLoading());
}


// 隐藏全局加载
function hideLoading() {
    $("#loadingId").remove();
}


